<?php
class Admin_AdminNewsCategoriesController extends  Zendvn_Controller_Action{

	private $_dirUpload;
	
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/admin/white');		
		$this->_templateObj->load();	
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		//$ss->unsetAll();
		$ss->setExpirationSeconds(600);
		$this->_arrParam['arrFilter'] = $ss->getIterator();
		
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] = $pageNumber;
		$this->_paning['rowCount'] = $this->_configObj->getSite('list_limit');		
		$this->_paning['pageRange'] = 3;				
		$this->_arrParam['panigator'] = $this->_paning;
		
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
		$this->view->Title = $this->view->translate('Manager') . ' : '
							 .  $this->view->translate('News Categories') . ' : '
							 .  $this->view->translate('List');
		$this->view->headTitle($this->view->Title,true);
		
		$tblCat 				= new Zendvn_Model_Table_NewsCategory();
		$this->view->Items 		= $tblCat->listAll($this->_arrParam);
		$this->view->slbLevel 	= $tblCat->itemInSelect($this->_arrParam,array('task'=>'admin-add'));
		$totalItem 				= $tblCat->totalItem($this->_arrParam);
		$this->view->tblCate 	= $tblCat;
		
		$panigator = Zendvn_Paginator::create($this->_arrParam['panigator'],$totalItem);		
		$this->view->panigator = $panigator;		
	}
	
	public function addAction(){
		$this->view->Title = $this->view->translate('Manager') . ' : '
							 .  $this->view->translate('News Category') . ' : '
							 .  $this->view->translate('Add');
		$this->view->headTitle($this->view->Title,true);
		$tblCat = new Zendvn_Model_Table_NewsCategory();
		$this->view->slbParents = $tblCat->itemInSelect($this->_arrParam,array('task'=>'admin-add'));
		if($this->_request->isPost()){			
			$validator = new Admin_Form_ValidateNewsCategory($this->_arrParam);			
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item 	= $validator->getData();				
			}else{
				
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));
				$data =  $validator->getData();	
				$tblCat->saveItem($data,array('task'=>'admin-add'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController . '/' . $this->_arrParam['action']);
				}
			}			
		}
		$this->_helper->viewRenderer('form');
	}
	
	public function editAction(){
		$this->view->Title = $this->view->translate('Manager') . ' : '
							 .  $this->view->translate('News Category') . ' : '
							 .  $this->view->translate('Edit');
		$this->view->headTitle($this->view->Title,true);
		if($this->_arrParam['id'] == 1) $this->_redirect($this->_mainAction);
		if($this->_request->isPost()){			
			$validator = new Admin_Form_ValidateNewsCategory($this->_arrParam);			
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item 	= $validator->getData();				
			}else{
				
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));
				$data =  $validator->getData();	
									
				$tblCat = new Zendvn_Model_Table_NewsCategory();
				$tblCat->saveItem($data,array('task'=>'admin-edit'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController 
									 . '/' . $this->_arrParam['action'] 
									 . '/id/' . $this->_arrParam['id']);
				}
			}			
		}
		$tblCat = new Zendvn_Model_Table_NewsCategory();
		$this->view->Item = $tblCat->getItem($this->_arrParam);
		$this->view->slbParents = $tblCat->itemInSelect($this->_arrParam);
		$this->_helper->viewRenderer('form');
	}
	public function deleteAction(){
		if($this->_arrParam['id'] == 1) $this->_redirect($this->_mainAction);
		$tblCat = new Zendvn_Model_Table_NewsCategory();
		
		if($this->_arrParam['type'] == 'multi'){
			$tblCat->deleteItem($this->_arrParam, array('task'=>'multi'));
		}else{
			$tblCat->deleteItem($this->_arrParam);
		}
		$this->_helper->flashMessenger->addMessage(array('success'=>'The delete was successful'));
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
		
	}
	public function infoAction(){
		$this->view->Title = $this->view->translate('Manager') . ' : '
							 .  $this->view->translate('News Category') . ' : '
							 .  $this->view->translate('Info');
		$this->view->headTitle($this->view->Title,true);
		if($this->_arrParam['id'] == 1) $this->_redirect($this->_mainAction);
		$tblCat = new Zendvn_Model_Table_NewsCategory();
		$this->view->Item = $tblCat->getItem($this->_arrParam,array('task'=>'info'));				
	}
	public function statusAction(){
		
		$tblCat = new Zendvn_Model_Table_NewsCategory();
		
		if($this->_arrParam['type'] == 'multi'){
			$tblCat->statusItem($this->_arrParam,array('task'=>'multi'));
		}else{
			$tblCat->statusItem($this->_arrParam);
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function sortAction(){		
		
		if($this->_request->isPost()){
			$tblCat = new Zendvn_Model_Table_NewsCategory();
			$tblCat->sortItem($this->_arrParam);
			$this->_redirect($this->_mainAction);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function filterAction(){
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		
		if($this->_arrParam['type'] == 'search'){
			$ss->keyword = $this->_arrParam['keyword'];
			$ss->searchField = $this->_arrParam['search-field'];
		}
		
		if($this->_arrParam['type'] == 'clear'){
			$ss->keyword = '';
			$ss->searchField = '';
		}
		
		if($this->_arrParam['type'] == 'filter'){
			$ss->status 	= $this->_arrParam['status'];
			$ss->level 		= $this->_arrParam['level'];
			$ss->parentNode = $this->_arrParam['parentNode'];
		}
		
		if($this->_arrParam['type'] == 'sort'){
			$ss->col = $this->_arrParam['col'];
			$ss->order = $this->_arrParam['order'];
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function removePicAction(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrParam['module'],
													 	  'fileConfig'=>'news-category.ini'));
		
		$upload = new Zendvn_File_Upload();
		$tblCat = new Zendvn_Model_Table_NewsCategory();		
		$pictureConfig = $config->getConfig('category');
		$upload->remove($pictureConfig['uploadDir'],$this->_arrParam['file']);
		$upload->remove($pictureConfig['thumbDir'],$this->_arrParam['file']);			
		$tblCat->update(array('image'=>''), 'id = ' . $this->_arrParam['id']);
					
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();
	}	
	
}